package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.*;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.maintenance.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/scheme")
public class DeviceInspectionplanController {
    @Autowired
    private DevicePatrolschemeService devicePatrolschemeService;

    @Autowired
    private InspectionDetailsService inspectionDetailsService;
    @Autowired
    private DeviceRulesService deviceRulesService;
    @Autowired
    private DeviceInspectiontaskService deviceInspectiontaskService;
    @Autowired
    private DocumentationService documentationService;
    @Autowired
    private CodeRules codeRules;

    private String code;

    @GetMapping("/schemeList")
    public Page<DevicePatrolscheme> getSupplierList(@RequestParam(defaultValue = "1") int page,
                                                    @RequestParam(defaultValue = "2") int size,
                                                    @RequestParam(defaultValue = "") String condition,
                                                    @RequestParam(defaultValue = "") String type,
                                                    @RequestParam(defaultValue = "") String name
    ) {
        System.out.println("condition:" + condition);
        System.out.println("page:" + page);
        return devicePatrolschemeService.schemeList(page, size, condition, type, name);
    }

    @RequestMapping("/addPatrol")
    @Transactional
    public String add(@RequestBody Map<String, Object> map) throws ParseException {
        if (map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.isEmpty()) {
            //返回错误信息
            return "TableName is null or empty";
        } else {
            System.out.println("1map:" + map);
            DevicePatrolscheme devicePatrolscheme = new DevicePatrolscheme();
            devicePatrolscheme.setCode(code());
            code = devicePatrolscheme.getCode();

            // 使用 Integer.valueOf 转换整数
            devicePatrolscheme.setPatrolschemeName((String) map.get("patrolSchemeName"));
            devicePatrolscheme.setPatrolschemeRule(Integer.valueOf(map.get("patrolSchemeRule").toString()));
            devicePatrolscheme.setPatrolschemePerson((String) map.get("patrolSchemePerson"));
            devicePatrolscheme.setPatrolschemeDaytypeId((String) map.get("patrolSchemeDaytypeId"));
            devicePatrolscheme.setPatrolschemeClass((String) map.get("patrolSchemeClass"));
            devicePatrolscheme.setPatrolschemeType((String) map.get("patrolSchemeType"));
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date patrolSchemeOntime = sdf.parse((String) map.get("patrolSchemeOntime"));
            Date patrolSchemeUntime = sdf.parse((String) map.get("patrolSchemeUntime"));
            devicePatrolscheme.setInsertPerson((String) map.get("name"));
            devicePatrolscheme.setInsertTime(new Date());
            devicePatrolscheme.setPatrolschemeOntime(patrolSchemeOntime);
            devicePatrolscheme.setPatrolschemeUntime(patrolSchemeUntime);
            devicePatrolscheme.setNotas((String) map.get("notas"));
            System.out.println("scheme:" + devicePatrolscheme);
            return devicePatrolschemeService.addDevicePatrol(devicePatrolscheme);
        }

    }

    public String code() {
        String TableName = "device_patrolscheme";
        List<DeviceRules> list = deviceRulesService.getDeviceRulesByTableName(TableName);
        String s = codeRules.codeRules(list);
        return s;
    }

    @RequestMapping("/addDetail")
    @Transactional
    public String addDetail(@RequestBody Map<String, Object> map) {
        System.out.println("2map:" + map);
        System.out.println(code);
        if (map.isEmpty()) {
            //返回错误信息
            return "TableName is null or empty";
        } else {
            InspectionDetails inspectionDetails = new InspectionDetails();
            inspectionDetails.setCode((String) map.get("deviceCode"));
            inspectionDetails.setCodeType(code);
            inspectionDetails.setWhether(Integer.valueOf(map.get("isCritical").toString()));
            inspectionDetails.setCheckIn(Integer.valueOf(map.get("isScan").toString()));
            inspectionDetails.setProject(map.get("inspectionCode").toString());
            System.out.println("details:" + inspectionDetails);
            return inspectionDetailsService.addInspectionDetail(inspectionDetails);
        }

    }

    @RequestMapping("/addTask")
    @Transactional
    public String addTask(@RequestBody Map<String, Object> map) throws ParseException {
        System.out.println("111111" + code);
        if (map.isEmpty()) {
            return "参数不能为空";
        }
        // 检查输入的表名称是否为空
        if (map.isEmpty()) {
            //返回错误信息
            return "TableName is null or empty";
        } else {
            List<DeviceRules> list = deviceRulesService.getDeviceRulesByTableName("device_inspectiontask");
            DeviceInspectiontask deviceInspectiontask = new DeviceInspectiontask();
            deviceInspectiontask.setInspectiontaskTypeId("未执行");
            deviceInspectiontask.setInspectiontaskName((String) map.get("patrolSchemeName"));
            deviceInspectiontask.setCode(codeRules.codeRules(list));
            deviceInspectiontask.setInspectiontaskCode(code);
            deviceInspectiontask.setInspectiontaskDaytypeId((String) map.get("patrolSchemeDaytypeId"));
            deviceInspectiontask.setInspectiontaskNumber("01");
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date patrolSchemeOntime = sdf.parse((String) map.get("patrolSchemeOntime"));
            Date patrolSchemeUntime = sdf.parse((String) map.get("patrolSchemeUntime"));
            deviceInspectiontask.setInspectiontaskOntime(patrolSchemeOntime);
            deviceInspectiontask.setInspectiontaskUntime(patrolSchemeUntime);
            deviceInspectiontask.setInspectiontaskOkontime(patrolSchemeOntime);
            deviceInspectiontask.setInspectiontaskOkuntime(patrolSchemeUntime);
            long time1 = patrolSchemeOntime.getTime();
            long time2 = patrolSchemeUntime.getTime();
            long diff = Math.abs(time2 - time1) / (60 * 1000);
            deviceInspectiontask.setInsertPerson((String) map.get("name"));
            deviceInspectiontask.setInsertTime(new Date());
            deviceInspectiontask.setInspectiontaskTime(String.valueOf(diff));
            deviceInspectiontask.setInspectiontaskClass((String) map.get("patrolSchemeClass"));
            deviceInspectiontask.setInspectiontaskPerson((String) map.get("patrolSchemePerson"));
            return deviceInspectiontaskService.addTask(deviceInspectiontask);
        }


    }

    @RequestMapping("/adjust")
    public String adjust(@RequestBody Map<String, Object> map) {
        System.out.println("adjust:" + map);
        if (map.isEmpty()) {
            return "参数不能为空";
        } else {
            DevicePatrolscheme devicePatrolscheme = new DevicePatrolscheme();
            devicePatrolscheme.setCode((String) map.get("code"));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date updateTime = sdf.parse((String) map.get("updateTime"));
                devicePatrolscheme.setUpdateTime(updateTime);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            devicePatrolscheme.setCause((String) map.get("cause"));
            devicePatrolscheme.setPatrolschemeClass((String) map.get("patrolschemeClass"));
            devicePatrolscheme.setPatrolschemePerson((String) map.get("patrolschemePerson"));
            return devicePatrolschemeService.update(devicePatrolscheme);
        }

    }

    @RequestMapping("/upload")
    public String upload(@RequestBody Map<String, Object> map) {

        Documentation documentation = new Documentation();
        documentation.setCode(code);
        documentation.setFilename((String) map.get("name"));
        documentation.setCreatedBy((String) map.get("uploader"));
        documentation.setCreationTime(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date uploadDate = sdf.parse((String) map.get("uploadDate"));
            documentation.setUploadTime(uploadDate);

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        documentation.setUploader((String) map.get("uploader"));
        documentation.setSize((String) map.get("size"));
        System.out.println("document:" + map);
        System.out.println("documentssss:" + documentation);
        return documentationService.document(documentation);
    }

    @GetMapping("/documentQuery")
    public List<Documentation> documentations(@RequestParam("") String code) {
        System.out.println(11111);
        return documentationService.doucumentQuery(code);
    }

    @GetMapping("/del")
    public String del(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id, @RequestParam(value = "status", required = false, defaultValue = "0") Integer status, @RequestParam("") String code) {
        return devicePatrolschemeService.del(id, status, code);
    }
}
